package com.corpgovernment.organization.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.corpgovernment.organization.Bo.EmployeeOpenCardMQBo;
import com.corpgovernment.organization.metric.MetricService;
import com.corpgovernment.organization.mq.EmployeeOpenCardMQProducer;
import com.corpgovernment.organization.service.BookingRelationService;
import com.ctrip.corp.obt.generic.core.event.EventSubscriber;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class OpenCardSubscriber implements EventSubscriber<EmployeeActionEvent> {

    @Autowired
    private EmployeeOpenCardMQProducer employeeOpenCardMQProducer;
    @Autowired
    private BookingRelationService bookingRelationService;

    @Override
    public void onEvent(EmployeeActionEvent event) {
       //员工开卡
        try {
            openCard(event);
        }catch (Exception e){
            log.info("员工异步开卡失败:{}",e);
            MetricService.metricOrgOrEmployeeCounter(MetricService.ORGANIZATION_MANAGE_IMPORT_EMPLOYEE,false,MetricService.ASYNC);
            throw e;
        }
        MetricService.metricOrgOrEmployeeCounter(MetricService.ORGANIZATION_MANAGE_IMPORT_EMPLOYEE,true,MetricService.ASYNC);
    }

    /**
     * 员工开卡
     */
    public void openCard(EmployeeActionEvent event) {
        for (String uid : event.getUidList()) {
            EmployeeOpenCardMQBo employeeOpenCardMQBo = new EmployeeOpenCardMQBo();
            employeeOpenCardMQBo.setUid(uid);
            employeeOpenCardMQBo.setOrgId(event.getOrgId());
            employeeOpenCardMQBo.setActionType(event.getActionType());
            employeeOpenCardMQProducer.sendEmployeeOpenCard(employeeOpenCardMQBo);
        }
    }
}
